package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.mapper.OtcOrderDetailMapper;
import com.service.dao.model.OtcOrderDetail;
import com.service.scheduling.service.OtcOrderDetailService;
import com.service.scheduling.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service("hmOtcOrderDetailService")
public class OtcOrderDetailServiceImpl extends ServiceImpl<OtcOrderDetailMapper, OtcOrderDetail> implements OtcOrderDetailService {

    @Override
    public OtcOrderDetail getLastDealOrder(Date someDay) {
        Date someDayStartTime = DateUtil.getDayOfStart(someDay);
        Date someDayEndTime = DateUtil.getDayOfEnd(someDay);

        List<OtcOrderDetail> detailList = list(new QueryWrapper<OtcOrderDetail>().lambda()
                .ge(OtcOrderDetail::getCreateTime, someDayStartTime)
                .le(OtcOrderDetail::getCreateTime, someDayEndTime)
                .ge(OtcOrderDetail::getOtcCreateTime, someDayStartTime)
                .le(OtcOrderDetail::getOtcCreateTime, someDayEndTime)
                .orderByDesc(OtcOrderDetail::getId));

        return ObjectUtils.isEmpty(detailList) ? null : detailList.get(0);
    }

    @Override
    public BigDecimal getMaxPriceOrder(Date someDay, Integer coinsId) {
        Date someDayStartTime = DateUtil.getDayOfStart(someDay);
        Date someDayEndTime = DateUtil.getDayOfEnd(someDay);
        return baseMapper.selectMaxPriceOtcOrderByDate(coinsId, someDayStartTime, someDayEndTime);
    }

    @Override
    public BigDecimal getMinPriceOrder(Date someDay, Integer coinsId) {
        Date someDayStartTime = DateUtil.getDayOfStart(someDay);
        Date someDayEndTime = DateUtil.getDayOfEnd(someDay);
        return baseMapper.selectMinPriceOtcOrderByDate(coinsId, someDayStartTime, someDayEndTime);
    }
}
